//
//  ViewController.m
//  CAShapeLayer
//
//  Created by dome on 16/8/23.
//  Copyright © 2016年 Icengy. All rights reserved.
//

#import "ViewController.h"
#import "FanButtonView.h"
@interface ViewController ()
@property (nonatomic,strong)CAShapeLayer *shapeLayer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor lightGrayColor];
    FanButtonView *view=[[FanButtonView alloc]initWithFrame:CGRectMake(0, 0, 335, 335)];
    view.center=self.view.center;
    [self.view addSubview:view];
    view.backgroundColor=[UIColor blueColor];

    _shapeLayer=[CAShapeLayer layer];
    _shapeLayer.frame =CGRectMake(0, 0, 200, 200);
    _shapeLayer.position =self.view.center;
    _shapeLayer.path =[self getStartBezierPath].CGPath;
    _shapeLayer.fillColor=[UIColor clearColor].CGColor;
    _shapeLayer.strokeColor=[UIColor redColor].CGColor;
    
    _shapeLayer.lineWidth=2.f;
    [self.view.layer addSublayer:_shapeLayer];
    [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(pathAnimation) userInfo:nil repeats:YES];
    
    
    
    
}
-(void)pathAnimation
{
    static int i=0;
    if (i++ % 2 ==0) {
        CABasicAnimation *circleAnim=[CABasicAnimation animationWithKeyPath:@"path"];
        circleAnim.removedOnCompletion=NO;
        circleAnim.duration=1;
        circleAnim.fromValue=(__bridge id)[self getStartBezierPath].CGPath;
        circleAnim.toValue=(__bridge id)[self getStartBezierPath1].CGPath;
        _shapeLayer.path=[self getStartBezierPath1].CGPath;
        [_shapeLayer addAnimation:circleAnim forKey:@"animateCirclePath"];
    }else{
        CABasicAnimation *circleAnim=[CABasicAnimation animationWithKeyPath:@"path"];
        circleAnim.removedOnCompletion=NO;
        circleAnim.duration=1;
        circleAnim.fromValue=(__bridge id)[self getStartBezierPath1].CGPath;
        circleAnim.toValue=(__bridge id)[self getStartBezierPath].CGPath;
        _shapeLayer.path=[self getStartBezierPath].CGPath;
        [_shapeLayer addAnimation:circleAnim forKey:@"animateCirclePath"];
    }
}
-(UIBezierPath *)getStartBezierPath
{
    UIBezierPath *start =[UIBezierPath bezierPath];
    [start moveToPoint:CGPointMake(22.5, 2.5)];
    [start addLineToPoint:CGPointMake(28.32, 14.49)];
    [start addLineToPoint:CGPointMake(41.52, 16.32)];
    [start addLineToPoint:CGPointMake(31.92, 25.56)];
    [start addLineToPoint:CGPointMake(34.26, 38.68)];
    [start addLineToPoint:CGPointMake(22.5, 32.4)];
    [start addLineToPoint:CGPointMake(10.74, 38.68)];
    [start addLineToPoint:CGPointMake(13.08, 25.56)];
    [start addLineToPoint:CGPointMake(3.48, 16.32)];
    [start addLineToPoint:CGPointMake(16.68, 14.49)];

    [start closePath];
    return start;
}
-(UIBezierPath *)getStartBezierPath1
{
    UIBezierPath *start =[UIBezierPath bezierPath];
    [start moveToPoint:CGPointMake(22.5, 2.5)];
    [start addLineToPoint:CGPointMake(32.01, 9.41)];
    [start addLineToPoint:CGPointMake(41.52, 16.32)];
    [start addLineToPoint:CGPointMake(37.89, 27.5)];
    [start addLineToPoint:CGPointMake(34.26, 38.68)];
    [start addLineToPoint:CGPointMake(22.5, 38.68)];
    [start addLineToPoint:CGPointMake(10.74, 38.68)];
    [start addLineToPoint:CGPointMake(7.11, 27.5)];
    [start addLineToPoint:CGPointMake(3.48, 16.32)];
    [start addLineToPoint:CGPointMake(12.99, 9.41)];
    
    [start closePath];
    return start;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];


}

@end
